package ystp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ystp.service.FujianService;
import ystp.support.JsonResult;
import ystp.support.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("Fujian")
public class FujianController {

    @Autowired
    FujianService fujianService;

    public Logger log = LoggerFactory.getLogger(ShengchanqiyelinxuanController.class);

    @RequestMapping("/ViewAll")
    public String ViewAll(String xiangmuid,String xiangmuleixing,HttpServletRequest request, ModelMap map) {

        if (isLogin(request)) {
            map.put("xiangmuId", xiangmuid);
            map.put("xiangmuLeixing", xiangmuleixing);
            return "main/Fujian/view_fujian12";
        } else {
            return "main/login";
        }

    }

    @RequestMapping("bindviewList")
    @ResponseBody
    public JsonResult<?> bindviewList(@RequestParam("xiangmuid") String ppxiangmuId, @RequestParam("pageindex") int ppPageIndex,
                                      @RequestParam("pagesize") int ppPageSize,@RequestParam("xiangmuleixing") String ppxiangmuleixing){
        Integer mmCount;
        List<Map<String,Object>> mmresultlist = new ArrayList<>();
        try {
                List<Map<String, Object>> list = fujianService.selectPingui(ppxiangmuleixing, ppxiangmuId, ppPageIndex, ppPageSize);
                mmCount = fujianService.countPingui(ppxiangmuleixing, ppxiangmuId);
                for (int i = 0; i < list.size(); i++) {
                    int mmPinzhongHebingshu = 0;
                    Map<String, Object> mmMap = list.get(i);
                    if (mmMap != null) {
                        String mmPinzhongOne = mmMap.get("xuhao") == null ? "" : mmMap.get("xuhao").toString();
                        List<Map<String, Object>> mmPinguilist = fujianService.findShengchanqiyeByPingui(ppxiangmuleixing, ppxiangmuId, mmPinzhongOne);
                        if (mmPinguilist.size() > 0) {
                            mmPinzhongHebingshu = mmPinguilist.size();
                        } else {
                            mmPinzhongHebingshu = mmPinzhongHebingshu + 1;
                        }
                        mmMap.put("pinguilist", mmPinguilist);
                        mmMap.put("pinzhonghebinghang", mmPinzhongHebingshu);
                    }
                    mmresultlist.add(mmMap);
                }

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("list", list);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("Fujian/bindviewList:error",e);
            return JsonResult.getErrorResult("Fujian/bindviewList:error " + e.getMessage());
        }
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult<?> delete(@RequestParam("xiangmuid") String ppxiangmuId,@RequestParam("xiangmuleixing") String ppxiangmuleixing){
        try {
            return fujianService.deleteByxiangmuId(ppxiangmuId,ppxiangmuleixing)==true? JsonResult.getSuccessResult("删除材料成功")
                    : JsonResult.getErrorResult("删除材料失败");
        }catch (Exception e) {
            e.printStackTrace();
            log.error("Fujian/delete:error",e);
            return JsonResult.getErrorResult("Fujian/delete:error " + e.getMessage());
        }
    }
    public boolean isLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("Id") == null) {
            return false;
        }
        return true;
    }
}
